TRAPEZOID
Overview
The TRAPEZOID function performs numerical integration of sampled data using the composite trapezoidal rule, one of the most fundamental and widely-used techniques in numerical integration. This method approximates the area under a curve by dividing it into trapezoids and summing their areas, making it ideal for integrating discrete data points where an analytical solution is unavailable.
The trapezoidal rule belongs to the Newton–Cotes formulas family of numerical integration methods. It works by approximating the region under a function f(x) between consecutive sample points as a trapezoid. For a pair of points (x_{k-1}, y_{k-1}) and (x_k, y_k), the area of each trapezoid is:
A_k = \frac{y_{k-1} + y_k}{2} \cdot (x_k - x_{k-1})
The composite (or “chained”) trapezoidal rule sums the areas of all trapezoids across the integration interval:
\int_a^b f(x)\,dx \approx \sum_{k=1}^{N} \frac{f(x_{k-1}) + f(x_k)}{2} \Delta x_k
This implementation uses SciPy’s trapezoid function from the scipy.integrate module (see GitHub source). The function accepts non-uniformly spaced sample points, computing the appropriate \Delta x_k = x_k - x_{k-1} for each interval.
The trapezoidal rule has an error bound proportional to the second derivative of the function. For a uniformly spaced grid with step size h over N intervals, the error is:
E = -\frac{(b-a)^3}{12N^2} f''(\xi)
for some \xi \in [a, b]. This makes the method second-order accurate (O(N^{-2})), meaning doubling the number of sample points reduces the error by approximately a factor of four. The method converges particularly rapidly for periodic functions integrated over their periods.
Common applications include calculating the area under experimental data curves, computing work from force-displacement measurements, and approximating integrals in physics and engineering when only discrete measurements are available.
This example function is provided as-is without any representation of accuracy.
Excel Usage
=TRAPEZOID(y, x)
y(list[list], required): Single column of y values (dependent variable) at sampled pointsx(list[list], required): Single column of x values (independent variable) at sampled points
Returns (float): Integral value (float), or error message string.
Examples
Example 1: Demo case 1
Inputs:
| y | x |
|---|---|
| 1 | 0 |
| 4 | 1 |
| 9 | 2 |
Excel formula:
=TRAPEZOID({1;4;9}, {0;1;2})
Expected output:
9
Example 2: Demo case 2
Inputs:
| y | x |
|---|---|
| 1 | 4 |
| 2 | 6 |
| 3 | 8 |
Excel formula:
=TRAPEZOID({1;2;3}, {4;6;8})
Expected output:
8
Example 3: Demo case 3
Inputs:
| y | x |
|---|---|
| 1 | 0 |
| 1 | 1 |
| 1 | 2 |
Excel formula:
=TRAPEZOID({1;1;1}, {0;1;2})
Expected output:
2
Example 4: Demo case 4
Inputs:
| y | x |
|---|---|
| -1 | 0 |
| 0 | 1 |
| 1 | 2 |
Excel formula:
=TRAPEZOID({-1;0;1}, {0;1;2})
Expected output:
0
Python Code
from scipy.integrate import trapezoid as scipy_trapezoid
def trapezoid(y, x):
"""
Integrate sampled data using the composite trapezoidal rule.
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.trapezoid.html
This example function is provided as-is without any representation of accuracy.
Args:
y (list[list]): Single column of y values (dependent variable) at sampled points
x (list[list]): Single column of x values (independent variable) at sampled points
Returns:
float: Integral value (float), or error message string.
"""
def _normalize_column(data, name):
# Normalize scalar to single-element 2D list and validate a single-column 2D list
if isinstance(data, (int, float, bool)):
data = [[data]]
if not isinstance(data, list):
return f"Invalid input: {name} must be a 2D list with at least two rows."
if len(data) < 2:
return f"Invalid input: {name} must be a 2D list with at least two rows."
column = []
for row in data:
if not isinstance(row, list) or len(row) != 1:
return f"Invalid input: {name} must be a single column 2D list."
try:
numeric = float(row[0])
except Exception:
return f"Invalid input: {name} must contain numeric values."
column.append(numeric)
return column
y_values = _normalize_column(y, "y")
if isinstance(y_values, str):
return y_values
x_values = _normalize_column(x, "x")
if isinstance(x_values, str):
return x_values
if len(x_values) != len(y_values):
return "Invalid input: x must have the same number of rows as y."
# Compute trapezoidal integration using SciPy.
try:
result = scipy_trapezoid(y_values, x=x_values)
return float(result)
except Exception as exc:
return f"scipy.integrate.trapezoid error: {exc}"